package be.redo.model.entities;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.format.annotation.NumberFormat;

/**
 * @author Frank Plain old Java class to represent a database table row.
 *         Represents a flight reservation in the domain model.
 * 
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalReservations", query = "select count(r) from Reservation r"),
		@NamedQuery(name = "findAllReservations", query = "select r from Reservation r order by r.vacation"),
		@NamedQuery(name = "findReservationsByFlight", query = "select r from Reservation r where r.flight = :flight") })
public class Reservation implements Serializable {

	/**
	 * Generated serialVersionUID.
	 */
	private static final long serialVersionUID = 4581483756499061615L;

	/**
	 * Price of the flight is euros.
	 */
	@Min(0)
	@NotNull
	@NumberFormat(pattern = "#,##0.00")
	private BigDecimal price;

	/**
	 * Vacation for this reservation.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "vacation")
	private Vacation vacation;

	/**
	 * Reserved flight.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "flight")
	private Flight flight;

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public Vacation getVacation() {
		return this.vacation;
	}

	public void setVacation(Vacation vacation) {
		this.vacation = vacation;
	}

	public Flight getFlight() {
		return this.flight;
	}

	public void setFlight(Flight flight) {
		this.flight = flight;
	}

	/**
	 * Generated key by the Database
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "reservation_id")
	private Long reservationId;

	public Long getReservationId() {
		return this.reservationId;
	}

	public void setReservationId(Long id) {
		this.reservationId = id;
	}

	/**
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((flight == null) ? 0 : flight.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result
				+ ((vacation == null) ? 0 : vacation.hashCode());
		return result;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Reservation other = (Reservation) obj;
		if (flight == null) {
			if (other.flight != null) {
				return false;
			}
		} else if (!flight.equals(other.flight)) {
			return false;
		}
		if (price == null) {
			if (other.price != null) {
				return false;
			}
		} else if (!price.equals(other.price)) {
			return false;
		}
		if (vacation == null) {
			if (other.vacation != null) {
				return false;
			}
		} else if (!vacation.equals(other.vacation)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
